package org.jeecg.modules.zcgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.zcgl.entity.AssetFixed;
import org.jeecg.modules.zcgl.entity.AssetFixedLog;
import org.jeecg.modules.zcgl.mapper.AssetFixedLogMapper;
import org.jeecg.modules.zcgl.service.IAssetFixedLogService;
import org.jeecg.modules.zcgl.service.IAssetFixedService;
import org.jeecg.modules.zcgl.vo.statistical.AssetBorrowVo;
import org.jeecg.modules.zcgl.vo.statistical.AssetReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 固定资产记录
 * @Author: jeecg-boot
 * @Date:   2021-07-02
 * @Version: V1.0
 */
@Service
public class AssetFixedLogServiceImpl extends ServiceImpl<AssetFixedLogMapper, AssetFixedLog> implements IAssetFixedLogService {
    @Autowired
    private IAssetFixedService assetFixedService;

    @Override
    public IPage<AssetBorrowVo> queryAssetFixedBorrowPageList(Page<AssetBorrowVo> page, QueryWrapper<AssetBorrowVo> queryWrapper) {
        return this.baseMapper.queryAssetFixedBorrowPageList(page,queryWrapper);
    }

    @Override
    public IPage<AssetReturnVo> queryAssetFixedReturnPageList(Page<AssetReturnVo> page, QueryWrapper<AssetReturnVo> queryWrapper) {
        return this.baseMapper.queryAssetFixedReturnPageList(page,queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void borrow(AssetFixedLog assetFixedLog) {
        AssetFixed assetFixed = assetFixedService.getById(assetFixedLog.getAssetFixedId());
        if(assetFixed !=null){
            assetFixed.setStatus("2");
            assetFixed.setAssetFixedCategoryId(assetFixedLog.getNowCategoryId());
            assetFixedService.updateById(assetFixed);
            assetFixedLog.setStatus(CommonConstant.STATUS_1);
            this.updateById(assetFixedLog);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void returnAsset(AssetFixedLog assetFixedLog) {
        AssetFixed assetFixed = assetFixedService.getById(assetFixedLog.getAssetFixedId());
        if(assetFixed !=null){
            assetFixed.setStatus(CommonConstant.STATUS_1);
            assetFixed.setAssetFixedCategoryId(assetFixedLog.getNowCategoryId());
            assetFixedService.updateById(assetFixed);
            assetFixedLog.setStatus(CommonConstant.STATUS_1);
            this.updateById(assetFixedLog);
        }
    }
}
